package com.ffms.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ffms.entity.Admin;


import com.ffms.entity.FamilyMember;
import com.ffms.service.AdminService;
import com.ffms.service.FamilyMemberService;
import com.ffms.service.impl.AdminServiceImpl;
import com.ffms.service.impl.FamilyMemberServiceImpl;

//登录控制层
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1.接受视图层的表单
        String userid = request.getParameter("userid");
        String password = request.getParameter("pwd1");
        String role = request.getParameter("role");
        //保存当前用户角色到session，目的是控制权限菜单的显示
        request.getSession().setAttribute("role", role);
        //如果role对应管理员，则进行管理员登录
        if ("admin".equals(role)) {
            //2.调用业务层并返回结果，判断是否登录成功
            AdminService adminService = new AdminServiceImpl();
            Admin admin = adminService.login(userid, password);
            Admin admin1 = adminService.loginByName(userid);
            Admin admin2 = adminService.loginBypwd(password);
            //保存用户名,于注销之前一直有效
            request.getSession().setAttribute("user", admin);
            //3.根据结果跳转到相应页面，成功至主页，失败至登录页面
            if (admin != null) {
                //获取网站的访问人数与之前的访问总人数
                ServletContext context = request.getServletContext();
                Integer count2 = (Integer) context.getAttribute("count");//null
                //如果是第一个用户，人数为一，如果不是，则加一
                if (count2 == null) {
                    count2 = 1;
                } else {
                    count2++;
                }
                context.setAttribute("count", count2);
                response.sendRedirect(request.getContextPath() + "/index.jsp");
//  			登陆成功，跳转到主页
            } else {
                if (admin1 == null) {
                    //用户名未查询到，跳转至注册页面
                    request.setAttribute("error", "管理员用户名不存在,");
                    request.setAttribute("error2", "<a href=\"/ffms/admin/adminAdd.jsp\">点击此注册</a>");
                    // 登陆失败，跳转至登录页面
                    RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
                    rd.forward(request, response);
                    //RequestDispatcher rd = request.getRequestDispatcher("admin/updatePwd.jsp");
                    //rd.forward(request, response);
                    return;
                } if(admin1!=null&&admin2==null) {
//                    密码错误,跳转至主页面找回密码
                    request.setAttribute("error", "密码错误");
                    request.setAttribute("error1", "<a href=\"/ffms/admin/updatePwd.jsp?role=admin\">找回密码</a>");
                    RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
                    rd.forward(request, response);
                    return;
                }
            }
        }
        //如果对应家庭成员，则家庭成员登录
        else if ("fm".equals(role)) {
            //2.调用业务层并返回结果，判断是否登录成功
            FamilyMemberService familyMemberService = new FamilyMemberServiceImpl();
            FamilyMember member = familyMemberService.login(userid, password);
            FamilyMember member1 = familyMemberService.loginByName(userid);
            FamilyMember member2 = familyMemberService.loginBypwd(password);
            //保存用户名,于注销之前一直有效
            request.getSession().setAttribute("user", member);
            //3.根据结果跳转到相应页面，成功至主页，失败至登录页面
            if (member != null) {
                // 获取网站的访问人数与之前的访问总人数
                ServletContext context = request.getServletContext();
                Integer count2 = (Integer) context.getAttribute("count");//null
            //如果是第一个用户，人数为一，如果不是，则加一
                if (count2 == null) {
                    count2 = 1;
                } else {
                    count2++;
                }
                context.setAttribute("count", count2);
                response.sendRedirect(request.getContextPath() + "/index.jsp");
            } else {
                if (member1 == null) {
                    //用户名不存在
                    request.setAttribute("error", "用户名不存在,请联系管理员注册");
                    // 登陆失败，跳转至登录页面
                    RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
                    rd.forward(request, response);
                    return;
                } if(member1!=null&&member2==null) {
                    //密码错误
                    request.setAttribute("error", "密码错误");
                    request.setAttribute("error1", "<a href=\"/ffms/admin/updatePwd.jsp?role=fm\">找回密码</a>");
                    RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
                    rd.forward(request, response);
                    return;
                }
            }
        }
    }

}

